
from shopping_mall.core import db_handle


class ShopMall:

    @classmethod
    def show_goods(cls) -> list:
        goods = db_handle.get_goods()
        for index, value in enumerate(goods):
            print(index + 1, ".", value["good_name"], value["good_price"])
        return goods

    @classmethod
    def join_shop_cart(cls, **kw):
        shop_cart_data = db_handle.get_shop_cart()
        if shop_cart_data:
            for good in shop_cart_data:
                if good["good_name"] == kw["good_name"]:
                    good["quantity"] += 1
                else:
                    shop_cart_data.append(kw)
        else:
            shop_cart_data.append(kw)
        db_handle.write_shop_cart(*shop_cart_data)

    @classmethod
    def show_shop_cart(cls):
        shop_cart_data = db_handle.get_shop_cart()
        for good in shop_cart_data:
            print("名称:", good["good_name"], "价格:", good["good_price"], "数量:", good["quantity"])
        return shop_cart_data

    @classmethod
    def clear_shop_cart(cls):
        db_handle.write_shop_cart(*[])
